﻿unit SmOrderChargeModel;

interface

uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm;

type

  TSmOrderChargeModel = class(TObject)
  public 
    const cModuleKey='SmOrderCharge'; 
    const cTblName ='sm_order_charge';
    const cBaseView = 'view_sm_order_charge';
    const cObjNameKey='ObjName';
    const cObjGUIDKey='ObjGUID';
    const cObjItemsKey='Items';
    const cCdsModelKey='CdsModel';
    
    class function GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetQuerySQL(const iCondition:string; const dOrderBy:string=''; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;

    const cOrderChargeIdKey='order_charge_id';
    const cOrderIdKey='order_id';
    const cOrderChargeSortNoKey='order_charge_sort_no';
    const cChargeItemIdKey='charge_item_id';
    const cOrderChargeAmountKey='order_charge_amount';
    const cOrderChargeRemarkKey='order_charge_remark';
  end;
  
  TSmOrderCharge = class(TObject)
  private    
    fOrderChargeId:string;
    fOrderId:string;
    fOrderChargeSortNo:Integer;
    fChargeItemId:string;
    fOrderChargeAmount:Real;
    fOrderChargeRemark:string;
    
  protected
    fObjGUID:string;
    fInitDate:TDateTime;
    fCdsModel:TClientDataSet;
    function ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    property ObjGUID:string read fObjGUID;
    property OrderChargeId:string read fOrderChargeId write fOrderChargeId;
    property OrderId:string read fOrderId write fOrderId;
    property OrderChargeSortNo:Integer read fOrderChargeSortNo write fOrderChargeSortNo;
    property ChargeItemId:string read fChargeItemId write fChargeItemId;
    property OrderChargeAmount:Real read fOrderChargeAmount write fOrderChargeAmount;
    property OrderChargeRemark:string read fOrderChargeRemark write fOrderChargeRemark;
  end;

  TSmOrderChargeList = class(TDictionary<string,TSmOrderCharge>)
  private
    fCdsModel:TClientDataSet;
    fSortKeys:TStrings;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    function FindByKey(const iKey:string):TSmOrderCharge;
    function IsExist(iKey:string):Boolean;
    function Add(const iItem:TSmOrderCharge):string;
    function Delete(const iKey:string):Boolean;
    function Remove(const iKey:string):TSmOrderCharge;
    procedure Clear;
    
    property SortKeys:TStrings read fSortKeys write fSortKeys;
  end;

implementation

class function TSmOrderChargeModel.GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TSmOrderChargeModel.cTblName;
    dblmTable: ReadName:=TSmOrderChargeModel.cTblName;
    dblmView: ReadName:=TSmOrderChargeModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s;'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s WHERE %s;'),[ReadName,iCondition]);
  end;
end;

class function TSmOrderChargeModel.GetQuerySQL(const iCondition:string; const dOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TSmOrderChargeModel.cTblName;
    dblmTable: ReadName:=TSmOrderChargeModel.cTblName;
    dblmView: ReadName:=TSmOrderChargeModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT * FROM %s %s;'),[ReadName,
                                                 IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s %s;'),[ReadName,iCondition,
                                                            IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end;
end;

class function TSmOrderChargeModel.GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TSmOrderChargeModel.cTblName;
    dblmTable: ReadName:=TSmOrderChargeModel.cTblName;
    dblmView: ReadName:=TSmOrderChargeModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin      
    Result:=Format(string('SELECT * FROM %s'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s'),[ReadName,iCondition]);
  end;
  Result:=Format(string('SELECT TOP %d * FROM (SELECT row_number() OVER(ORDER BY %s) AS _row_no_,* FROM (%s) AS A) AS B WHERE _row_no_>%d ORDER BY _row_no_'),
                  [iPageSize,
                  IfThen(SameStr(Trim(iOrderBy),EmptyStr),'',iOrderBy),
                  Result,
                  (iPageNo-1)*iPageSize]);
end;

constructor TSmOrderCharge.Create();
begin
  inherited Create;
  fInitDate:=EncodeDateTime( 1899, 12, 30, 0, 0, 0, 0 );
  fCdsModel:=TClientDataSet.Create(nil);
  ResetObject;
end;

destructor TSmOrderCharge.Destroy;
begin
  FreeAndNil(fCdsModel);
  inherited Destroy;
end;

procedure TSmOrderCharge.ResetObject;
begin
  fObjGUID:=TSK0001Comm.GetGUID;
  fOrderChargeId:=EmptyStr;
  fOrderId:=EmptyStr;
  fOrderChargeSortNo:=-1;
  fChargeItemId:=EmptyStr;
  fOrderChargeAmount:=-1;
  fOrderChargeRemark:=EmptyStr;
end;

function TSmOrderCharge.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    ResetObject;
    if (iCds.FindField(TSmOrderChargeModel.cOrderChargeIdKey)<>nil) then
    begin
      fOrderChargeId:=iCds.FieldByName(TSmOrderChargeModel.cOrderChargeIdKey).AsString;     
      fObjGUID:=TSK0001Comm.SetObjGUID(fOrderChargeId);
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderChargeModel.cOrderChargeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderChargeModel.cOrderIdKey)<>nil) then
    begin
      fOrderId:=iCds.FieldByName(TSmOrderChargeModel.cOrderIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderChargeModel.cOrderIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderChargeModel.cOrderChargeSortNoKey)<>nil) then
    begin
      fOrderChargeSortNo:=iCds.FieldByName(TSmOrderChargeModel.cOrderChargeSortNoKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderChargeModel.cOrderChargeSortNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderChargeModel.cChargeItemIdKey)<>nil) then
    begin
      fChargeItemId:=iCds.FieldByName(TSmOrderChargeModel.cChargeItemIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderChargeModel.cChargeItemIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderChargeModel.cOrderChargeAmountKey)<>nil) then
    begin
      fOrderChargeAmount:=iCds.FieldByName(TSmOrderChargeModel.cOrderChargeAmountKey).AsFloat;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderChargeModel.cOrderChargeAmountKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderChargeModel.cOrderChargeRemarkKey)<>nil) then
    begin
      fOrderChargeRemark:=iCds.FieldByName(TSmOrderChargeModel.cOrderChargeRemarkKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderChargeModel.cOrderChargeRemarkKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmOrderCharge.ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    if (iCds.FindField(TSmOrderChargeModel.cOrderChargeIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderChargeModel.cOrderChargeIdKey).AsString:=fOrderChargeId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderChargeModel.cOrderChargeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderChargeModel.cOrderIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderChargeModel.cOrderIdKey).AsString:=fOrderId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderChargeModel.cOrderIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderChargeModel.cOrderChargeSortNoKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderChargeModel.cOrderChargeSortNoKey).AsInteger:=fOrderChargeSortNo;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderChargeModel.cOrderChargeSortNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderChargeModel.cChargeItemIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderChargeModel.cChargeItemIdKey).AsString:=fChargeItemId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderChargeModel.cChargeItemIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderChargeModel.cOrderChargeAmountKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderChargeModel.cOrderChargeAmountKey).AsFloat:=fOrderChargeAmount;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderChargeModel.cOrderChargeAmountKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderChargeModel.cOrderChargeRemarkKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderChargeModel.cOrderChargeRemarkKey).AsString:=fOrderChargeRemark;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderChargeModel.cOrderChargeRemarkKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.ObjectToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmOrderCharge.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    iCds.Append;
    if ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmOrderCharge.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TSmOrderChargeModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TSmOrderChargeModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数！错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性节点.'),[Self.ClassName,TSmOrderChargeModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    ResetObject;
      
    if (Json.ItemByName(TSmOrderChargeModel.cCdsModelKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TSmOrderChargeModel.cCdsModelKey).AsString),EmptyStr) then
    begin
      TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TSmOrderChargeModel.cCdsModelKey).AsString),MemStream);
      if MemStream.Size>0 then
      begin
        fCdsModel.LoadFromStream(MemStream);
      end;
    end;
    
    if (Json.ItemByName(TSmOrderChargeModel.cObjGUIDKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TSmOrderChargeModel.cObjGUIDKey).AsString),EmptyStr) then
    begin
      fObjGUID:=Json.ItemByName(TSmOrderChargeModel.cObjGUIDKey).AsString;
    end;
    
    if Json.ItemByName('OrderChargeId')<>nil then
    begin
      fOrderChargeId:=Json.ItemByName('OrderChargeId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrderChargeId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrderId')<>nil then
    begin
      fOrderId:=Json.ItemByName('OrderId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrderId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrderChargeSortNo')<>nil then
    begin
      fOrderChargeSortNo:=Json.ItemByName('OrderChargeSortNo').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrderChargeSortNo']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ChargeItemId')<>nil then
    begin
      fChargeItemId:=Json.ItemByName('ChargeItemId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ChargeItemId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrderChargeAmount')<>nil then
    begin
      fOrderChargeAmount:=Json.ItemByName('OrderChargeAmount').AsFloat;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrderChargeAmount']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrderChargeRemark')<>nil then
    begin
      fOrderChargeRemark:=Json.ItemByName('OrderChargeRemark').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrderChargeRemark']);
      Result:=False;
      Exit;  
    end;
    
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TSmOrderCharge.SaveToStr():string;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;  
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create; 
  try
    Json.Add('ModuleKey').AsString:=TSmOrderChargeModel.cModuleKey;
    Json.Add(TSmOrderChargeModel.cObjNameKey).AsString:=Self.ClassName;
    
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TSmOrderChargeModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));
    
    Json.Add(TSmOrderChargeModel.cObjGUIDKey).AsString:=fObjGUID;
    Json.Add('OrderChargeId').AsString:=fOrderChargeId;
    Json.Add('OrderId').AsString:=fOrderId;
    Json.Add('OrderChargeSortNo').AsInteger:=fOrderChargeSortNo;
    Json.Add('ChargeItemId').AsString:=fChargeItemId;
    Json.Add('OrderChargeAmount').AsFloat:=fOrderChargeAmount;
    Json.Add('OrderChargeRemark').AsString:=fOrderChargeRemark;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

constructor TSmOrderChargeList.Create();
begin
  inherited Create;
  fCdsModel:=TClientDataSet.Create(nil);
  fSortKeys:=TStringList.Create;
  ResetObject;
end;

destructor TSmOrderChargeList.Destroy;
begin
  FreeAndNil(fCdsModel);
  Clear;
  FreeAndNil(fSortKeys);
  inherited Destroy;
end;

procedure TSmOrderChargeList.ResetObject;
begin
  Clear;
end;

function TSmOrderChargeList.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Item:TSmOrderCharge;
begin
  ResetObject;
  if iCds<>nil then
  begin
    iCds.First;
    while not iCds.Eof do
    begin
      Item:=TSmOrderCharge.Create;
      if Item.LoadFromCDS(iCds,oError) then
      begin
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end else begin
        Result:=False;
        Exit;
      end;
      iCds.Next;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误! 错误内容传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmOrderChargeList.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Key:string;
begin
  if (iCds<>nil) then
  begin
  for Key in fSortKeys do
  begin
    iCds.Append;
    if Items[Key].ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误! 错误内容:传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmOrderChargeList.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json,ItemJson,ArrayJson:TQJson;
  Item:TSmOrderCharge;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TSmOrderChargeModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TSmOrderChargeModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:缺少%s属性节点.'),[Self.ClassName,TSmOrderChargeModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    if (Json.ItemByName(TSmOrderChargeModel.cObjItemsKey)<>nil) and
      (Json.ItemByName(TSmOrderChargeModel.cObjItemsKey).IsArray) then
    begin
      ResetObject;
      if Json.ItemByName(TSmOrderChargeModel.cCdsModelKey)<>nil then
      begin
        TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TSmOrderChargeModel.cCdsModelKey).AsString),MemStream);
        if MemStream.Size>0 then
        begin
          fCdsModel.LoadFromStream(MemStream);
        end;
      end;
      ArrayJson:=Json.ItemByName(TSmOrderChargeModel.cObjItemsKey);
      for ItemJson in ArrayJson do
      begin
        Item:=TSmOrderCharge.Create;
        Item.LoadFromStr(ItemJson.AsString,oError);
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end;
    end else begin
      Result:=False;
      Exit;
    end;
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TSmOrderChargeList.SaveToStr():string;
var
  Key:string;
  Json,ItemJson,ArrayJson:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    Json.Add(TSmOrderChargeModel.cObjNameKey).AsString:=Self.ClassName;
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TSmOrderChargeModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));

    ArrayJson:=Json.Add(TSmOrderChargeModel.cObjItemsKey,jdtArray);
    for Key in fSortKeys do
    begin
      ItemJson:=TQJson.Create;
      if ItemJson.TryParse(Items[Key].SaveToStr()) then
      begin
        ArrayJson.Add(ItemJson);
      end else begin
        ItemJson.Free;
      end;
    end;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

function TSmOrderChargeList.FindByKey(const iKey:string):TSmOrderCharge;
var
  Item:TSmOrderCharge;
begin
  if TryGetValue(iKey,Item) then
  begin
    Result:=Item;
  end else begin
    Result:=nil;
  end;
end;

function TSmOrderChargeList.IsExist(iKey:string):Boolean;
var
  Item:TSmOrderCharge;
begin
  Result:= TryGetValue(iKey,Item);
end;

function TSmOrderChargeList.Add(const iItem:TSmOrderCharge):string;
begin
  if iItem.ObjGUID<>EmptyStr then
  begin
    inherited Add(iItem.ObjGUID,iItem);
    Result:=iItem.ObjGUID;
    fSortKeys.Add(iItem.ObjGUID);
  end else begin
    Result:=EmptyStr;
  end;
end;

function TSmOrderChargeList.Delete(const iKey:string):Boolean;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Remove(iKey).Free;
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    Result:=True;
  end else begin
    Result:=False;
  end;
end;

function TSmOrderChargeList.Remove(const iKey:string):TSmOrderCharge;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Result:=Items[iKey];
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    inherited Remove(iKey);
  end else begin
    Result:=nil;
  end;
end;

procedure TSmOrderChargeList.Clear;
var
  Key:string;
begin
  for Key in Keys do
  begin
    if Items[Key]<>nil then
    begin
      Items[Key].Free;
    end;
  end;
  fSortKeys.Clear;
  inherited Clear;
end;

end.
